package com.ruoyi.system.controller;

import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.system.domain.SysProduct;
import com.ruoyi.system.service.ISysProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 商品信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/product")
public class SysProductController extends BaseController {

    @Autowired
    private ISysProductService productService;

    /**
     * 查询商品数据集合
     *
     * @param product 商品信息
     * @return 商品数据集合
     */
    @PreAuthorize(hasPermi = "system:product:list")
    @GetMapping("/list")
    public TableDataInfo list(SysProduct product) {
        startPage();
        List<SysProduct> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    /**
     * 通过商品ID查询商品信息
     *
     * @param productId 商品ID
     */
    @PreAuthorize(hasPermi = "system:product:query")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable Long productId) {
        return AjaxResult.success(productService.selectProductById(productId));
    }

    /**
     * 批量删除商品信息
     *
     * @param productIds 需要删除的商品ID
     * @return 结果
     */
    @PreAuthorize(hasPermi = "system:product:remove")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds) {
        return toAjax(productService.deleteProductByIds(productIds));
    }

    /**
     * 修改商品信息
     *
     * @param product 商品信息
     * @return 结果
     */
    @PreAuthorize(hasPermi = "system:product:edit")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysProduct product) {
        if (UserConstants.NOT_UNIQUE.equals(productService.checkProductNameUnique(product))) {
            return AjaxResult.error("修改商品'" + product.getProductName() + "'失败，商品名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(productService.checkProductCodeUnique(product))) {
            return AjaxResult.error("修改商品'" + product.getProductCode() + "'失败，商品编码已存在");
        }
        product.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 新增商品信息
     *
     * @param product 商品信息
     * @return 结果
     */
    @PreAuthorize(hasPermi = "system:product:add")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysProduct product) {
        if (UserConstants.NOT_UNIQUE.equals(productService.checkProductNameUnique(product))) {
            return AjaxResult.error("添加商品'" + product.getProductName() + "'失败，商品名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(productService.checkProductCodeUnique(product))) {
            return AjaxResult.error("添加商品'" + product.getProductCode() + "'失败，商品编码已存在");
        }
        product.setCreateBy(SecurityUtils.getUsername());
        return toAjax(productService.insertProduct(product));
    }

    @PreAuthorize(hasPermi = "system:post:export")
    @Log(title = "商品管理导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysProduct product) throws IOException {
        List<SysProduct> list = productService.selectProductList(product);
        ExcelUtil<SysProduct> util = new ExcelUtil<SysProduct>(SysProduct.class);
        util.exportExcel(response, list, "商品数据");
    }
}
